package com.baizhi.cmfz.listener;

import com.alibaba.fastjson.JSON;
import com.baizhi.cmfz.dao.LogDao;
import com.baizhi.cmfz.entity.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;

@Component
public class LogListener {
    @Autowired(required = false)
    private LogDao logDao;
    @JmsListener(destination = "cmfz-queue")
    public void logListerner(Message message) {
        TextMessage textMessage = (TextMessage) message;
        try {
            String text = textMessage.getText();
            //将获取到的json串序列化为对象，添加到数据库
            Log log = JSON.parseObject(text,Log.class);
            System.out.println("json转换为对象"+log);
            logDao.addLog(log);
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}
